package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 员工控制器
 */
@RestController
@RequestMapping("/emp")
@Api(tags = "员工管理", description = "员工操作接口")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;




    /**
     * 后台系统登录功能
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody @Validated(Employee.LoginEmployee.class) Employee employee){
        //登录
        return employeeService.login(employee);
    }


    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询所有员工信息", notes="查询所有员工信息，get请求")
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 查询所有
     */
    @GetMapping("/shopEmployees")
    @ApiOperation(value = "查询所有员工信息", notes="查询所有员工信息，get请求")
    public List<Employee> shopEmployees(){
        return employeeService.shopEmployees();
    }

    /**
     * 查询所有
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "查询部门分页数据", notes="查询数据")
    public PageList<Employee> findPage(@RequestBody EmployeeQuery employeeQuery){
        //查询分页数据
        return employeeService.findPage(employeeQuery);
    }

    /**
     * 新增或修改的操作
     * 根据id来区分新增和修改
     * 怎么区分请求使用的请求方式  这里是post
     */
    @PostMapping
    public AjaxResult saveOrUpdate(@RequestBody @Valid Employee employee){

        //对所有的入参进行一个有效判断
        /*Optional.ofNullable(employee.getName()).orElseThrow(() -> new RuntimeException("部门名称不能为空"));

        if (StringUtils.isBlank(employee.getSn())){
            throw  new RuntimeException("部门编号不能为空");
        }
*/
        try {
            if (employee.getId() == null){
                //没有id就是新增
                employeeService.save(employee);
            }else{
                //修改
                employeeService.update(employee);
            }
            //返回成功对象
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败对象
            return AjaxResult.createError("操作异常,向日葵！");
        }
    }
    /**
     * 删除操作
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            //删除
            employeeService.delete(id);
            //返回成功对象
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败对象
            return AjaxResult.createError("操作异常,向日葵！");
        }
    }
    /**
     * 批量删除操作
     */
    @PostMapping("/batchDelete")
    public AjaxResult delete(@RequestBody EmployeeQuery employeeQuery){

        try {
            //批量删除操作
            employeeService.batchDelete(employeeQuery);
            //返回成功对象
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败对象
            return AjaxResult.createError("操作异常,向日葵！");
        }
    }

}
